﻿using SQLite;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using System.Collections.ObjectModel;
using Cashbook.Models;
using System;
using System.Threading.Tasks;
using Cashbook.ViewModels;

namespace Cashbook.Services
{
	public class StatementsDataAccess
	{
		readonly SQLiteAsyncConnection database;
       
		//public ObservableCollection<Statement> Statements { get; set; }
		public StatementsDataAccess()
		{
			database = Database.GetDBConnection();

		}
        		


		public Task<List<Statement>> GetStatementsAsync(int billId)
		{
            return database.Table<Statement>().Where(m=>m.BillId == billId)
                           .OrderByDescending(m=>m.AddedDate).ToListAsync();
		}

        		

		public Task<Statement> GetStatementAsync(int id)
		{
			return database.Table<Statement>().Where(i => i.Id == id).FirstOrDefaultAsync();
		}

		public Task<int> SaveStatementAsync(Statement item)
		{

			if (item.Id != 0)
			{
				return database.UpdateAsync(item);
			}
			else
			{
				return database.InsertAsync(item);
			}

		}


		public Task<int> DeleteStatementAsync(Statement item)
		{
			return database.DeleteAsync(item);
		}
	}
}
